package com.ajguan.library.view;

import com.ajguan.library.IRefreshHeader;
import com.ajguan.library.ResourceTable;
import com.ajguan.library.State;
import com.github.ybq.core.SpinKitView;
import com.github.ybq.core.SpriteFactory;
import com.github.ybq.core.Style;
import com.github.ybq.core.sprite.Sprite;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.app.Context;


public class SimpleRefreshHeaderView extends StackLayout implements IRefreshHeader {


    private static final String TAG = SimpleRefreshHeaderView.class.getSimpleName();
    private AnimatorProperty rotateUp;
    private AnimatorProperty rotateDown;
    private AnimatorProperty rotateInfinite;
    private Text textView;
    private Component arrowIcon;
    private Image successIcon;
    private SpinKitView loadingIcon;

    public SimpleRefreshHeaderView(Context context) {
        this(context, null);
    }


    public SimpleRefreshHeaderView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    //ACCELERATE_DECELERATE
    public SimpleRefreshHeaderView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_default_refresh_header, this, true);
        textView = (Text) findComponentById(ResourceTable.Id_text);
        arrowIcon = findComponentById(ResourceTable.Id_arrowIcon);
        successIcon = (Image) findComponentById(ResourceTable.Id_successIcon);
        loadingIcon = (SpinKitView) findComponentById(ResourceTable.Id_loadingIcon);
        Sprite sprite = SpriteFactory.create(Style.CIRCLE);
        sprite.setPaintColor(context.getColor(ResourceTable.Color_accent));
        sprite.setBounds(loadingIcon.getPaddingLeft(),
                loadingIcon.getPaddingTop(),
                loadingIcon.getWidth() - loadingIcon.getPaddingRight(),
                loadingIcon.getHeight() - loadingIcon.getPaddingBottom());
        sprite.setComponent(loadingIcon);
        loadingIcon.setIndeterminate(true);
        loadingIcon.addDrawTask((component, canvas) -> sprite.drawToCanvas(canvas));

        // 初始化动画
        rotateUp = arrowIcon.createAnimatorProperty()
                .rotate(180)
                .setCurveType(Animator.CurveType.LINEAR)
                .setTarget(arrowIcon)
                .setDuration(150);

        rotateDown = arrowIcon.createAnimatorProperty()
                .setDuration(150)
                .setCurveType(Animator.CurveType.LINEAR)
                .setTarget(arrowIcon)
                .rotate(0);

        rotateInfinite = loadingIcon.createAnimatorProperty()
                .setDuration(1500)
                .setLoopedCount(AnimatorProperty.INFINITE)
                .setCurveType(Animator.CurveType.LINEAR)
                .setTarget(loadingIcon)
                .rotate(360);
    }

    @Override
    public void reset() {
        textView.setText(ResourceTable.String_header_reset);
        successIcon.setVisibility(INVISIBLE);
        arrowIcon.setVisibility(VISIBLE);
        loadingIcon.setVisibility(INVISIBLE);
        rotateUp.reset();
        rotateDown.reset();
        rotateInfinite.reset();
    }

    @Override
    public void pull() {

    }

    @Override
    public void refreshing() {
        arrowIcon.setVisibility(INVISIBLE);
        loadingIcon.setVisibility(VISIBLE);
        textView.setText(ResourceTable.String_header_refreshing);
        rotateUp.cancel();
        rotateDown.cancel();
        rotateInfinite.start();
    }

    @Override
    public void onPositionChange(float currentPos, float lastPos, float refreshPos, boolean isTouch, State state) {
        // 往上拉
        if (currentPos < refreshPos ) {
            // Logger.getLogger(TAG).info(">>>>up");
            if (isTouch && state == State.PULL) {
                textView.setText(ResourceTable.String_header_pull);
                rotateUp.cancel();
                rotateDown.start();
            }
            // 往下拉
        } else if (currentPos > refreshPos && lastPos <= refreshPos) {
            // Logger.getLogger(TAG).info(">>>>down");
            if (isTouch && state == State.PULL) {
                textView.setText(ResourceTable.String_header_pull_over);
                rotateDown.cancel();
                rotateUp.start();
            }
        }
    }

    @Override
    public void complete() {
        loadingIcon.setVisibility(INVISIBLE);
        rotateUp.cancel();
        rotateDown.cancel();
        rotateInfinite.cancel();
        successIcon.setVisibility(VISIBLE);
        textView.setText(ResourceTable.String_header_completed);
    }
}
